<?php  if ( ! defined('DLFMPATH')) exit('No direct script access allowed');

class Initmodel extends DL_Model {
	
	var $tbl_name = "";

	function __construct()
	{
		// Call the Model constructor
		parent::__construct();
		$this->db->cache_delete_all();
		$this->load->helper('date');
		$this->load->helper('array');
		$this->load->helper('common');
	}
	
	function set_table_name($table_name){
		$this->tbl_name = $table_name;
	}
	
	function get_table_name(){
		return $this->tbl_name;
	}
	
	function set_free_result($query) {
		if($query) $query->free_result();
	}
			
	function fetch_data($query){
		$result = "";
		if($query->num_rows() > 0){
			foreach($query->result_array() as $row){
				$result[] = $row;
			}
		}
		return $result;
	}
	
	function fetch_data_with_desc($query){
		$result = "";
		if($query->num_rows() > 0){
			foreach($query->result_array() as $row){
				$row["num_rows"] = $query->num_rows() ;
				$result[] = $row;
			}
		}
		return $result;
	}

	function load_record($fullload=false) {
		$query = $this->db->get($this->tbl_name);
		//echo "<br>sql : ".$this->db->last_query();
		$result =  ($fullload) ? $this->fetch_data_with_desc($query) : $this->fetch_data($query);
		$this->set_free_result($query);
		if(is_var_array($result) && count($result) == 1){
			return $result[0];
		}else{
			return "";
		}
	}

	function load_records($fullload=false) {
		$query = $this->db->get($this->tbl_name);
		//echo "<br>sql : ".$this->db->last_query();
		$result =  ($fullload) ? $this->fetch_data_with_desc($query) : $this->fetch_data($query);
		$this->set_free_result($query);
		return $result;
	}
	
	function set_order_by($data=""){
		if( is_var_array($data) ){
			foreach($data as $key => $value){
				$this->db->order_by($key,$value);
			}
		}else if( !is_blank($data) ){
			$this->db->order_by($data);
		}
	}
	
	function set_group_by($data="") {
		if( !empty($data) ){
			$this->db->group_by($data);
		}
	}
	
	function set_where($data){
		if( is_var_array($data) ){
			$where_str = "( ";
		   	$i=0;
			foreach($data as $key => $value){
				if($i != 0) $where_str .= " AND ";
				$where_str .= " $key = '$value' ";
				$i++;
				//$this->db->where($key,$value);
			}
			$where_str .= " )";
			$this->db->where($where_str);
		}else if( !is_blank($data) ){
			$this->db->where($data);
		}
	}

	function set_where_not_equal($data){
		if( is_var_array($data) ){
			foreach($data as $key => $value){
				$this->db->where($key." != ",$value);
			}
		}else if( !is_blank($data) ){
			$this->db->where($data);
		}
	}

	function set_where_in($data){
		if( is_var_array($data) ){
			foreach($data as $key => $value){
				$this->db->where_in($key,$value);
			}
		}
	}
	
	function set_like($data,$wc_postion="both"){
		if( is_var_array($data) ){
			$where_str = "( ";
		   	$i=0;
			foreach($data as $key => $value){
				if($i != 0) $where_str .= " AND ";
				switch($wc_postion){
					case "both" : $where_str .= " $key like '%$value%' "; break;
					case "left" : $where_str .= " $key like '%$value' "; break;
					case "right" : $where_str .= " $key like '$value%' "; break;
					default : $where_str .= " $key like '%$value%' "; break;
				}
				$i++;
			}
			$where_str .= " )";
			$this->db->where($where_str);
		}
	}
	
	function set_or_like($data,$wc_postion="both"){
		if( is_var_array($data) ){
			$where_str = "( ";
		   	$i=0;
			foreach($data as $key => $value){
				if($i != 0) $where_str .= " OR ";
				switch($wc_postion){
					case "both" : $where_str .= " $key like '%$value%' "; break;
					case "left" : $where_str .= " $key like '%$value' "; break;
					case "right" : $where_str .= " $key like '$value%' "; break;
					default : $where_str .= " $key like '%$value%' "; break;
				}
				$i++;
			}
			$where_str .= " )";
			$this->db->where($where_str);
		}
	}
	
	function set_not_like($data,$wc_postion="both"){
		if( is_var_array($data) ){
			$where_str = "( ";
		   	$i=0;
			foreach($data as $key => $value){
				if($i != 0) $where_str .= " AND ";
				switch($wc_postion){
					case "both" : $where_str .= " $key not like '%$value%' "; break;
					case "left" : $where_str .= " $key not like '%$value' "; break;
					case "right" : $where_str .= " $key not like '$value%' "; break;
					default : $where_str .= " $key not like '%$value%' "; break;
				}
				$i++;
			}
			$where_str .= " )";
			$this->db->where($where_str);
		}
	}
	
	function set_or_not_like($data,$wc_postion="both"){
		if( is_var_array($data) ){
			$where_str = "( ";
		   	$i=0;
			foreach($data as $key => $value){
				if($i != 0) $where_str .= " OR ";
				switch($wc_postion){
					case "both" : $where_str .= " $key not like '%$value%' "; break;
					case "left" : $where_str .= " $key not like '%$value' "; break;
					case "right" : $where_str .= " $key not like '$value%' "; break;
					default : $where_str .= " $key not like '%$value%' "; break;
				}
				$i++;
			}
			$where_str .= " )";
			$this->db->where($where_str);
		}
	}
	
	function set_limit($limit_start=0,$no_record=0){
		if( !is_blank($limit_start) && !is_blank($no_record) ){
			$this->db->limit($no_record, $limit_start);
		}else if( !is_blank($no_record) ){
			$this->db->limit($no_record);
		}
	}

	function insert_record($data) {
		if($this->db->field_exists('created_date',$this->tbl_name)) $data['created_date'] = get_db_now();
		if($this->db->field_exists('created_by',$this->tbl_name)) $data['created_by'] = getSessionUserAid();
		$this->db->insert($this->tbl_name, $data);
		return $this->db->insert_id();
	}

	function update_record($data) {
		if( !is_var_array($data) ) return "";
		if($this->db->field_exists('updated_date',$this->tbl_name)) $data['updated_date'] = get_db_now();
		// if($this->db->field_exists('updated_by',$this->tbl_name)) $data['updated_by'] = getSessionUserAid();
		return $this->db->update($this->tbl_name, $data);
  }
	
	function delete_records() {
		$this->db->delete($this->tbl_name);
	}

}

/* End of file initmodel.php */
/* Location: ./application/model/initmodel.php */